#!/usr/bin/env python
#
# python-icontact2 - A python implementation of IContact API 2.0
# Copyright (c) 2010,2011 Alvin Delagon (http://www.alvinatorsplayground.blogspot.com/)
# All rights reserved.
#

from icontact import IContactSession
from ConfigParser import ConfigParser

if __name__ == "__main__":
    c = ConfigParser()
    c.read("config.cfg")
    url = c.get("sandbox", "base_url")
    username = c.get("sandbox", "username")
    password = c.get("sandbox", "password")
    appId = c.get("sandbox", "appId")
    accountId = c.get("sandbox", "accountId")
    folderId = c.get("sandbox", "folderId")

    # Obtaining your iContact accountId and folderId
    ic = IContactSession(url, appId, username, password)
    accountIds = ic.getAccountIds()
    accountId = accountIds[0]
    ic.setAccountId(accountId)
    folderIds = ic.getFolderIds()
    folderId = folderIds[0]
    ic.setFolderId(folderId)

    # Creating an iContact Session knowing your accountId and folderId
    ic = IContactSession(url, appId, username, password,
                         accountId=accountId, folderId=folderId)

    # IContact calls returns IContactResponse
    resp = ic.getContacts()
    # HTTP headers
    headers = resp.headers
    # Raw payload of the response
    raw_content = resp.raw_content
    # Payload of the response in JSON format
    json_content = resp.json_content

    # Get Contacts
    print ic.getContacts({'limit':10, 'orderby': 'createDate'}).prettyPrint()

    # Get Contact information
    print ic.getContact(1234567).prettyPrint()

    # Messages
    print ic.getMessages().prettyPrint()
    print ic.getMessage(123456).prettyPrint()
    print ic.getMessageStats(123456).prettyPrint()

    # Adding a Contact
    print ic.addContact({'email': 'foobar@gmail.com'}).prettyPrint()
